package com.ignou.mca.datasource;

//************************************************************//
//Encryption class used to encrypt and decrypt system's passwords//
//*************************************************************//
import javax.crypto.KeyGenerator;
import javax.crypto.Cipher;
import java.security.Key;
import java.security.SecureRandom;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder; //These objects are used to read and write
//to file system.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileNotFoundException;


public class Encryption {

	// Method to encrypt the password
	public static String encrypt(String password) throws Exception {
		// This class does the actual encryption
		Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
		// Initialise it using key got from the method below.
		cipher.init(Cipher.ENCRYPT_MODE, getKey());
		// Convert the password string to an array of bytes
		byte[] passwordAsBytes = password.getBytes("UTF8");
		// Encrypt
		byte[] encryptedPassword = cipher.doFinal(passwordAsBytes);
		// Convert the resulting array of bytes to ASCII form
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(encryptedPassword);
	}

	// Method to decrypt the password
	// Takes the ascii version of the password, and matches it to the character
	public static String decrypt(String encryptedPassword) throws Exception {
		// This class does the actual decryption
		Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
		// Initialise it using key got from the method below.
		cipher.init(Cipher.DECRYPT_MODE, getKey());
		// Convert ASCII characters to an array of bytes
		BASE64Decoder decoder = new BASE64Decoder();
		byte[] encryptedPasswordAsBytes = decoder
				.decodeBuffer(encryptedPassword);
		// Decrypt
		byte[] decryptedPassword = cipher.doFinal(encryptedPasswordAsBytes);
		// Convert the resulting array of bytes to ASCII form
		return new String(decryptedPassword, "UTF8");
	}

	/*
	 * Gets the Key used for encryting/decrypting.
	 * 
	 * The key is stored in a file because the same key used to encrypt a text
	 * MUST be used to decrypt it else you get a different result. The method is
	 * private because it is not used outside this class.
	 * 
	 * The method first looks for a file called "OnlineVoting.ser" if it finds
	 * it, it loads the key from there and returns it to whichever method asks
	 * for it. if it does not find the file, then it generates a new key, writes
	 * it to the file named above, then returns the key.
	 */
	private static Key getKey() throws Exception {
		Key key = null;
		try {
			// Attempt to find the key file. If it is not found,
			// Control passes to the catch block below.
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(
					"OnlineVoting.ser"));
			// File found, get the key from the file
			// You need to cast the object read from the
			// file as a key object.
			key = (Key) in.readObject();
			// close resource opened
			in.close();
		} catch (FileNotFoundException fnfe) {
			// key file doesn't exist, create key and store in file.
			KeyGenerator generator = KeyGenerator.getInstance("DES");
			generator.init(new SecureRandom());
			key = generator.generateKey();
//			ObjectOutputStream out = new ObjectOutputStream(
//					new FileOutputStream("OnlineVoting.ser"));
//			out.writeObject(key);
			//out.close();
		}
		return key;
	}

}
